//
//  MKVideoBFViewController.m
//  ManKeCartoon
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKVideoBFViewController.h"
#import "KrVideoPlayerController.h"
#import "MKBaseHeader.h"
@interface MKVideoBFViewController ()
@property (nonatomic, strong)KrVideoPlayerController *player;
@end

@implementation MKVideoBFViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [_titleView setBackgroundColor:[UIColor colorWithRed:0.91 green:0.42 blue:0.24 alpha:1]];
    [self.view addSubview:_titleView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 24, 100, 30)];
    
    
    titleLabel.text = self.dhName;
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    titleLabel.textColor = [UIColor whiteColor];
    
    [self.titleView addSubview:titleLabel];
    
    [self setUpBackButton];
    
    [self createWeb];
    [self createpageButton];
    
    
    
}


#pragma mark - 创建一个返回按钮
- (void)setUpBackButton
{
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setFrame:CGRectMake(10, 18, 30, 30)];
    [_backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.titleView addSubview:_backButton];
}
#pragma mark - 返回按钮点击方法
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)createWeb
{
    
    
    NSURL *url = [NSURL URLWithString:@"http://122.141.225.17/vlive.qqvideo.tc.qq.com/l0016pmnatd.mp4?platform=510303&br=58&fmt=mp4&vkey=347CC4E351C665EBA199A84A1183C7F7907B0F002326C5F4395061348F1E2731A10720E90D3B8DF10BFF6DED75088A68214A8FBCBF0B682F8E81812B48E23A69B73520189D92FFCF07D05D9A1BF3C737A9282B736532A549&level=30&sdtfrom=v5030"];
    
#pragma mark - 创建播放器
    if (!_player) {
        
        self.player = [[KrVideoPlayerController alloc] initWithFrame:CGRectMake(0, 64*HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT / 3)];
        
        [_player setDimissCompleteBlock:^{
            
            _player = nil;
            
        }];
        
        [_player setWillChangeToFullscreenMode:^{
            [self boorHidden:YES];
        }];
        
        [_player setWillBackOrientationPortrait:^{
            [self boorHidden:NO];
        }];
        
        _player.contentURL = url;
        [self.view addSubview:_player.view];
        
    }
    
    
    
    self.LineView = [[UIView alloc] initWithFrame:CGRectMake(0, 290*HEIGHT, SCREEN_WIDTH, 40)];
    [self.LineView setBackgroundColor:[UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1]];
    [self.view addSubview:self.LineView];
    
    self.muLu = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH / 2, 30)];
    [self.muLu setText:@"目录列表"];
    [self.muLu setTextColor:[UIColor blackColor]];
    [self.muLu setFont:[UIFont systemFontOfSize:14]];
    [self.LineView addSubview:self.muLu];
    

    
    self.pageScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.LineView.frame.size.height + self.LineView.frame.origin.y, SCREEN_WIDTH, SCREEN_HEIGHT - self.LineView.frame.size.height - self.LineView.frame.origin.y)];
    [self.pageScroll setBackgroundColor:[UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1]];
    [self.pageScroll setContentSize:CGSizeMake(0, 500)];
    [self.view addSubview:self.pageScroll];
    
    
}

#pragma mark - 全屏效果
-(void)boorHidden:(BOOL)hid
{
    
    self.LineView.hidden = hid;
    self.pageScroll.hidden = hid;
    self.navigationController.navigationBar.hidden = hid;
    
    
    [[UIApplication sharedApplication] setStatusBarHidden:hid withAnimation:hid];
}

-(void)createpageButton
{
    [MKAFNmodel getdataWithString:[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Cartoon/cartoonDetail/comic_id/%@",self.comic_id] block:^(id block) {
        
        self.AnimationArray = [[NSMutableArray alloc] init];
        
        for (NSDictionary *dic in [block objectForKey:@"data"]) {
            
            MKChartsModel *model = [[MKChartsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.AnimationArray addObject:model];
        }
        
      
        
        
        
        
        
        self.butArray = [[NSMutableArray alloc] init];
        
        for (int i = 0; i < self.AnimationArray.count ;i++) {
            
            
            self.pageButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.pageButton setFrame:CGRectMake(20 + (i % 4 * (SCREEN_WIDTH / 5 + 10)), i / 4 * 40 , SCREEN_WIDTH / 5, 30)];
            [self.pageButton.layer setMasksToBounds:YES];
            [self.pageButton.layer setCornerRadius:5];
            [self.pageButton setTitle:[NSString stringWithFormat:@"%ld",self.AnimationArray.count - i] forState:UIControlStateNormal];
            [self.pageButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.pageButton setBackgroundColor:[UIColor whiteColor]];
            self.pageButton.layer.borderWidth = .5;
            [self.pageButton.layer setBorderColor:[UIColor grayColor].CGColor];
            self.pageButton.titleLabel.font = [UIFont systemFontOfSize:15];
            [self.pageButton addTarget:self action:@selector(pageClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.pageScroll addSubview:self.pageButton];
            
            [self.butArray addObject:self.pageButton];
            
        }
        [self.pageScroll setContentSize:CGSizeMake(0, self.pageButton.frame.size.height + self.pageButton.frame.origin.y + 10)];
        
    }];
    
    
}



#pragma mark - 点击集数
-(void)pageClick:(UIButton *)sender
{
    
    [sender setBackgroundColor:[UIColor colorWithRed:0.90 green:0.2 blue:0.15 alpha:1]];
    
    for (UIButton *bu1 in self.butArray) {
        if (bu1 != sender) {
            [bu1 setBackgroundColor:[UIColor whiteColor]];
        }
    }
    
    NSLog(@"第%@集",sender.titleLabel.text);
   
    
    NSURL *url = [NSURL URLWithString:@"http://122.141.225.17/vlive.qqvideo.tc.qq.com/l0016pmnatd.mp4?platform=510303&br=58&fmt=mp4&vkey=347CC4E351C665EBA199A84A1183C7F7907B0F002326C5F4395061348F1E2731A10720E90D3B8DF10BFF6DED75088A68214A8FBCBF0B682F8E81812B48E23A69B73520189D92FFCF07D05D9A1BF3C737A9282B736532A549&level=30&sdtfrom=v5030"];
    
    _player.contentURL = url;
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
